//
//  TTView.m
//  Tint
//
//  Created by Kevin Wu on 2/22/12.
//  Copyright (c) 2012 Telligenty. All rights reserved.
//

#import "TTView.h"


@implementation TTView


#pragma mark - Accessors

@synthesize showTopShadow = _showTopShadow;

- (void)setShowTopShadow:(BOOL)showTopShadow {
    _showTopShadow = showTopShadow;
    [self setNeedsLayout];
}


@synthesize showBottomShadow = _showBottomShadow;

- (void)setShowBottomShadow:(BOOL)showBottomShadow {
    _showBottomShadow = showBottomShadow;
    [self setNeedsLayout];
}



#pragma mark - Memory

- (void)dealloc {
    TTRelease(_topShadowLayer);
    TTRelease(_bottomShadowLayer);
    [super dealloc];
}



#pragma mark - UIView

- (void)layoutSubviews {
	[super layoutSubviews];
    
    if (_showTopShadow) {
        if (!_topShadowLayer) {
            CAGradientLayer *shadowLayer = [self shadowAsInverse:YES];
            TTRetain(shadowLayer, _topShadowLayer);
            [self.layer addSublayer:_topShadowLayer];
        } else if ([self.layer.sublayers indexOfObjectIdenticalTo:_topShadowLayer] != 0) {
            [self.layer addSublayer:_topShadowLayer];
        }
        _topShadowLayer.frame = TTRectMake(0.0, 0.0 - _topShadowLayer.frame.size.height, self.width, _topShadowLayer.frame.size.height);
    } else {
        [_topShadowLayer removeFromSuperlayer];
        TTRelease(_topShadowLayer);
    }
    
    
    if (_showBottomShadow) {
        if (!_bottomShadowLayer) {
            CAGradientLayer *shadowLayer = [self shadowAsInverse:NO];
            TTRetain(shadowLayer, _bottomShadowLayer);
            [self.layer insertSublayer:_bottomShadowLayer atIndex:0];
        } else if ([self.layer.sublayers indexOfObjectIdenticalTo:_bottomShadowLayer] != 0) {
            [self.layer insertSublayer:_bottomShadowLayer atIndex:0];
        }
        _bottomShadowLayer.frame = TTRectMake(0.0, self.height, self.width, _bottomShadowLayer.frame.size.height);
    } else {
        [_bottomShadowLayer removeFromSuperlayer];
        TTRelease(_bottomShadowLayer);
    }
}

@end

